<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Language extends MY_Controller {
	
	public function __construct()
	{
		parent::__construct();
		$this->check_authentication(1);
		$this->load->model('Candidate_language_model', 'language');
		$this->lang->load('candidate/language', $this->session->userdata['user_lang']);
	}
	
	public function index()
	{
		$language_list = $this->language->get(array('idcandidate' => $this->session->userdata['idprofile']));
		       	
		$grid_language = array();
        
        foreach ($language_list->result() as $row)
		{
			$grid_language[] = '<div class="row"><span><span class="gtitle">'.$row->languagename.' ('.$row->levelname.')</span></span><div class="gtool"><a onclick="CandidateLanguage.editLanguage('.$row->id.');">'.$this->lang->line('global_update').'</a>
			 &middot; <a onclick="CandidateLanguage.deleteLanguage('.$row->id.');">'.$this->lang->line('global_delete').'</a></div></div>';
		}
		
		if (!$grid_language) {
			 $data['grid_language'] = '<center>'.$this->lang->line('language_noresults').'</center>';
		} else {
			 $data['grid_language'] = implode('<hr/>', $grid_language);
		}
		
		exitHTML($data['grid_language']);
	}
	
	public function insert()
	{
		$data = $_POST;
		
		if (missing_field($data, array('idlanguagetype', 'idlanguagelevel')))
		{
			formError($this->lang->line('language_missingfield'), true);
		}
		
		$data['idcandidate'] = $this->session->userdata['idprofile'];
		
		if ($this->language->get($data)->row())
		{
			formError($this->lang->line('language_languageexists'), true);
		}
		
		if ($this->language->insert($data) === false)
		{
		   formError($this->lang->line('global_systemfail'), true);
		}
		else
		{
			exitJs('CandidateLanguage.getLanguageGrid();closeBox();');
		}
	}
	
	public function update($id_language = '')
	{
		$data = $_POST;
		
		if (missing_field($data, array('idlanguagetype', 'idlanguagelevel')))
		{
			formError($this->lang->line('language_missingfield'), true);
		}
		
		if ($this->language->update($data) === false)
		{
		   formError($this->lang->line('global_systemfail'), true);
		}
		else
		{
			exitJs('CandidateLanguage.getLanguageGrid();closeBox();');
		}
	}
	
	public function delete()
	{
		$data = $_POST;
		
		if (missing_field($data, array('id')))
		{
			formError($this->lang->line('language_missingfield'));
		}
		
		if ($this->language->delete(array('id' => $data['id'])) === false)
		{
		   formError($this->lang->line('global_systemfail'));
		}
		else
		{
			exitJs('CandidateLanguage.getLanguageGrid();');
		}
	}
	
	public function edit($id_language = '')
	{
		header("Content-type: text/html; charset=utf-8");
		$data = $_POST;
		$data['id_language'] = $id_language;
		
		if (is_numeric($id_language))
		{
			$language_data = $this->language->get(array('idlanguage' => $id_language))->row();
			$data['extrascript']  = '$("#idlanguagetype").val("'.$language_data->idlanguage.'");';
			$data['extrascript'] .= '$("#idlanguagelevel").val("'.$language_data->idlanguagelevel.'");';
		}
		
		$data['userdata'] = $this->session->userdata;
        $this->load->view('candidate/language', $data);
	}
	
	public function add()
	{
		$this->edit();
	}
}

?>